package com.androidcourse.changeandget;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.androidcourse.changeandget.Database.DBHelper;
import com.androidcourse.changeandget.User.User;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class Login extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        DBHelper mSQlite = new DBHelper(Login.this);
        Button Login=(Button) findViewById(R.id.btnLogin);
        EditText UserName=(EditText) findViewById(R.id.edUser);
        EditText Pass=(EditText) findViewById(R.id.edPassword);
        DBHelper finalMSQlite = mSQlite;
        Login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String SID=UserName.getText().toString();
                String Password=Pass.getText().toString();

                if (!TextUtils.isEmpty(SID) && !TextUtils.isEmpty(Password)) {//判断账号密码输入是否为空
                    ArrayList<User> data = finalMSQlite.getAllDATA();
                    boolean userdata = false;
                    User thisuser=new User();//创建当前用户
                    for (int i = 0; i < data.size(); i++) {
                        User user = data.get(i);   //可存储账号数量
                        if (SID.equals(user.getId()) && Password.equals(user.getUserpwd())) {//判断数据库中是否有相同数据
                            userdata = true;
                            thisuser.setId(user.getId());//如果账号密码正确则把数据传入当前用户
                            thisuser.setUserpwd(user.getUserpwd());
                            thisuser.setSestate(user.getSestate());
                            thisuser.setSsex(user.getSsex());
                            thisuser.setStele(user.getStele());
                            thisuser.setUsername(user.getUsername());
                            break;
                        } else {
                            userdata = false;
                        }
                    }

                    if (userdata) {
                        Toast.makeText(Login.this, "登录成功", Toast.LENGTH_SHORT).show();
                        Intent intentlogin = new Intent(Login.this,homepage.class);
                        Bundle bundle=new Bundle();
                        bundle.putString("thisuser",thisuser.getId());
                        intentlogin = intentlogin.putExtras(bundle);
                        startActivity(intentlogin);//传入信息
                        finish();
                    } else {
                        Toast.makeText(Login.this, "用户名或密码不正确", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(Login.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
                }
            }
        });

        mSQlite = new DBHelper(Login.this);
    }
}